package com.sky.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Author mr.wu
 * @Data #{DATE} #{TIME}
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    private static final String SETMEAL_LIST="setmeal_list:";

    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal  setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        int s =  setmealMapper.insert(setmeal);
        if(s<1){
            throw new BaseException("保存套餐信息失败");
        }

        List<SetmealDish> setmealDishList= setmealDTO.getSetmealDishes();
        setmealDishList.forEach(x->{
            x.setSetmealId(setmeal.getId());
            setmealDishMapper.add(x);
        });

    }

    @Override
    public PageResult getPage(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<SetmealVO> list = setmealMapper.getPage(dto);
        Page<SetmealVO> page = (Page) list;
        Long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public int remove(List<Integer> ids) {
        ids.forEach(id->{
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus()==1){
                throw new BaseException("删除套餐中有起售的套餐，删除失败");
            }
        });
        return  setmealMapper.remove(ids);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id.intValue());
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        if(setmealDTO==null){
            throw new BaseException("要修改的套餐不存在");
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        int s =  setmealMapper.update(setmeal);
        if(s<1){
            throw new BaseException("修改套餐信息失败");
        }
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        //删除数据库中原有的套餐菜品信息
        setmealDishMapper.delete(setmeal.getId());
        if(!CollectionUtils.isEmpty(list)){
            //保存新的套餐菜品信息
            list.forEach(x->{
                x.setSetmealId(setmeal.getId());
                setmealDishMapper.add(x);
            });
        }
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        int s =  setmealMapper.update(setmeal);
        if(s<1){
            throw new BaseException("修改套餐状态失败");
        }
    }
    /**
     * PC端 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getPcSetmeal(Long categoryId) {
        String jsonStr = (String) redisTemplate.opsForHash().get(SETMEAL_LIST, SETMEAL_LIST + categoryId);
        if(!StringUtils.isEmpty(jsonStr)){
            List<Setmeal> result = JSON.parseArray(jsonStr, Setmeal.class);
            return result;
        }
        List<Setmeal> list  = setmealMapper.getPcSetmeal(categoryId);
        String jsonString = JSON.toJSONString(list);
        redisTemplate.opsForHash().put(SETMEAL_LIST, SETMEAL_LIST+categoryId, jsonString);
        return list;
    }
    /**
     * PC端 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getPcSetmealDish(Long id) {

       List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);
        List<DishItemVO> voList = list.stream().map(x -> {
            Dish dish = dishMapper.getPcDish(x.getDishId());
            DishItemVO vo = new DishItemVO();
            BeanUtils.copyProperties(x, vo);
            vo.setDescription(dish.getDescription());
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

}
